﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class site_setting_service : base_service
    {
        //get list language, status =-1: get all
        public PagedList<site_settings> adm_get_plist(int pageindex, int pagesize)//, int setting_type)
        {
            using (var data = new baseEntities())
            {
                var list = (from lg in data.tbl_site_settings
                           // where lg.setting_type == setting_type || setting_type == -1
                            orderby lg.setting_id ascending
                            select lg);
                list = list.OrderByDescending(p => p.setting_id);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        //get list language, to create setting file config
        public List<site_settings> get_list_config(int langid)
        {
            using (var data = new baseEntities())
            {
                var list = (from lg in data.tbl_site_settings
                            where lg.is_config == 1 && (lg.language_id == langid || lg.language_id == admin_const.NO_LANGUAGE)
                            orderby lg.setting_id ascending
                            select lg);
                return list.ToList();
            }
        }

        public int adm_setting_update(site_settings item)
        {
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_site_settings.Where(p => p.language_id == item.language_id &&
                                                                    p.setting_name.ToLower() == item.setting_name.ToLower()).FirstOrDefault();
                if (edit_item != null)
                { 
                    edit_item.setting_name = item.setting_name;
                    edit_item.setting_content = item.setting_content;
                    edit_item.date_modified = item.date_modified;
                    edit_item.language_id = item.language_id;
                  //  edit_item.setting_type = item.setting_type;
                    edit_item.is_config = item.is_config;

                    data.SaveChanges();
                    return edit_item.setting_id;
                }
                else
                {
                    data.AddTotbl_site_settings(item);
                    data.SaveChanges();
                    return item.setting_id;
                }
            }
        }

        //get items by id or code
        public site_settings getByID(string setting_name, int language_id)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_site_settings.Where(p => p.setting_name.ToLower() == setting_name.ToLower() &&
                                                            p.language_id == language_id).FirstOrDefault();
                if (item == null)
                    item = new site_settings();
                return item;
            }
        }

        public void deleteSetting(string setting_name, int language_id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_site_settings.Where(p => (p.language_id == language_id || language_id == -1) &&
                                                    p.setting_name.ToLower() == setting_name.ToLower()).FirstOrDefault();
                if (em != null)
                {
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }
    }
}
